package test.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class App {
	
	public static void main(String[] args) {
//		HelloService target = new HelloServiceImpl();
		Object proxy = Proxy.newProxyInstance(App.class.getClassLoader(), new Class<?>[]{HelloService.class}, new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println("before");
//				Object obj = method.invoke(target, args);
				Object obj = method.invoke(new Spy(), args);
				System.out.println("after");
				return obj;
			}
		});
		HelloService service = (HelloService)proxy;
		service.hello("zhangsan");
		
		printArray(HelloService.class.getInterfaces());
		split();
		printArray(new Class<?>[] {HelloService.class});
		split();
		printArray(new HelloServiceImpl().getClass().getInterfaces());
	}
	
	static void printArray(Object[] objs) {
		for(Object obj : objs) {
			System.out.println(obj);
		}
	}
	
	static void split() {
		System.out.println("-----------------------------------------");
	}

}

class Spy implements HelloService {
	
	public void hello(String name) {
		System.out.println("[spy]hello,"+name);
	}

	public void test() {
		System.out.println("[spy]test");
	}
	
}
